<?php
/**
 * 设置cookie
 *
 *
 */
class LibsCookie {
    /**
     * 增加cookie
     *
     * @param string $name name
     * @param string $value value 
     *
     * @return bool
     */
    public static function add($name, $value, $expire=0) {
        $tmpArr = array();
        if (isset($_COOKIE[$name])) {
            $tmpArr = unserialize($_COOKIE[$name]);
        }
        $tmpArr[] = $value;
        $tmpArr = array_unique($tmpArr);
        $expire = empty($expire) ? 0 : (time()+intval($expire));
        return setCookie($name, serialize($tmpArr), $expire);
    }

    /**
     * 获取cookie
     *
     * @param string name cookieName
     * @param string typeKey type
     *
     * @return array|string|null
     */
    public static function get($name, $typeKey='string') {
        if (!isset($_COOKIE[$name])) {
            return null;
        }
        switch($typeKey) {
        case 'array':
            return unserialize($_COOKIE[$name]);
        case 'string':
        default:
            return $_COOKIE[$name];
        }
        
    }
}
